from rest_framework.decorators import action
from rest_framework.permissions import AllowAny
from rest_framework.response import Response

import settings
from social.account.models import User
from social.other.models import Report, UpdateVersion
from social.other.serializers import UpdateVersionSerializer
from social.utils.oss import OssSystem
from social.utils.views import BaseGenericViewSet


class OtherViewSet(BaseGenericViewSet):
    permission_classes = (AllowAny,)
    queryset = User.objects
    oss_system = OssSystem(bucket_name=settings.OSS_BUCKET_NAME, end_point=settings.OSS_ENDPOINT)

    @action(methods=['POST'], detail=False)
    def oss(self, request, *args, **kwargs):
        result = self.oss_system.get_token(request.user)
        return Response(data=result)

    @action(methods=['POST'], detail=False)
    def report(self, request, *args, **kwargs):
        content = request.data.get('content')
        _type = request.data.get('type')
        extra = str(request.data.get('extra'))
        Report.objects.create(user=request.user, content=content, type=_type, extra=extra)
        return Response()

    @action(methods=['POST'], detail=False, permission_classes=(AllowAny,))
    def version(self, request, *args, **kwargs):
        ua = UpdateVersion.objects.order_by('-build').first()
        return Response(data=UpdateVersionSerializer(ua).data)
